package edu.pku.sei.modeler.uml.commands.component;

import edu.pku.sei.modeler.model.core.MetaModelElement;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.uml.BasicComponents.Component;
import edu.pku.sei.modeler.model.uml.Interfaces.Interface;
import edu.pku.sei.modeler.uml.commands.NestedElementDeleteCommand;

public class NestedComponentElementDeleteCommand extends
		NestedElementDeleteCommand {

	public NestedComponentElementDeleteCommand(MetaNode node,
			MetaNode fatherNode, boolean isDeletedFromDiagram) {
		super(node, fatherNode, isDeletedFromDiagram);
	}

	@Override
	public void deleteModelElement() {
		MetaModelElement modelElement = this.node.getModelElement();
		Component parentComponent = (Component) fatherNode.getModelElement();
		if (modelElement instanceof Component) {
			parentComponent.getOwnedMember().remove(modelElement);
		} else if (modelElement instanceof Interface) {
			parentComponent.getNestedInterface().remove(modelElement);
			if (((Interface)modelElement).getType() == Interface.TYPE_USEINTERFACE)
				parentComponent.getRequired().remove(modelElement);
			else if (((Interface)modelElement).getType() == Interface.TYPE_IMPLEMENTINTERFACE)
				parentComponent.getProvided().remove(modelElement);
		}
	}

	@Override
	public void undoDeleteModelElement() {
		MetaModelElement modelElement = this.node.getModelElement();
		Component parentComponent = (Component) fatherNode.getModelElement();
		if (modelElement instanceof Component) {
			parentComponent.getOwnedMember().add(modelElement);
		} else if (modelElement instanceof Interface) {
			parentComponent.getNestedInterface().add(modelElement);
			if (((Interface)modelElement).getType() == Interface.TYPE_USEINTERFACE)
				parentComponent.getRequired().add(modelElement);
			else if (((Interface)modelElement).getType() == Interface.TYPE_IMPLEMENTINTERFACE)
				parentComponent.getProvided().add(modelElement);
		}
	}
}
